package org.epidroid.epidroid.androsoul.ui.utils;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

import org.epidroid.epidroid.androsoul.engine.AndroSoul;
import org.epidroid.epidroid.androsoul.engine.service.IAndroSoulService;

import android.content.Context;
import android.util.Log;

public class ContactsLoader {

	private static final String FILE_SUFFIX = "-buddies";

	/**
	 * Load the contacts from the configuration file
	 */
	public static void loadContacts(IAndroSoulService _service, Context context) {
		FileInputStream fis = null;
		
		try {
			Log.v(AndroSoul.LOGCAT, "Loading contacts");
			fis = context.openFileInput(_service.getMySelf()
					.getName() + FILE_SUFFIX);
		} catch (FileNotFoundException e) {
			Log.v(AndroSoul.LOGCAT,
					"Could not load contacts, file does not exists");
		}
		_service.contactsLoad(fis);
	}

	/**
	 * Save the contacts to the configuration file
	 */
	public static void saveContacts(IAndroSoulService _service, Context context) {
		try {
			_service.contactsSave(context.openFileOutput(_service.getMySelf()
					.getName() + FILE_SUFFIX, Context.MODE_PRIVATE));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
}
